<template>
  <view>
    <button open-type="getUserInfo" @getuserinfo="wxLogin">点我登录</button>
  </view>
</template>

<script>
	export default{
		methods:{
			wxLogin(userInfoRes){
				// 用户信息
				console.log(userInfoRes.detail)
				let{encryptedData,iv,rawData,signature}=userInfoRes.detail
				// 获取code
				uni.login({
					success:async (loginRes)=>{
						console.log(loginRes)
						// 发请求
						let data = await this.$request({
							url:'/api/public/v1/users/wxlogin',
							method:'POST',
							data:{
								code:loginRes.code,
								encryptedData,
								iv,
								rawData,
								signature
							}
						})
						
						console.log(data)
						uni.setStorageSync('token',data.token)
						// 存用户信息
						uni.setStorageSync('userInfo',userInfoRes.detail.userInfo)
						
						// 返回上一页
						uni.navigateBack({
						})
					}
				})
			}
		}
	}
</script>

<style>
</style>